package com.littleHan.brushQuestions.web.start.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;


    /**
     * 用户注册
     *
     * @param generalUser 用户实例
     * @return
     * @throws FailedToInsertException
     */
    @RequestMapping(value = "/goRegisterJ", method = RequestMethod.POST)
    public String register(@RequestBody GeneralUser generalUser) throws FailedToInsertException {
        System.out.println(generalUser);
        registerService.generalUserRegister(generalUser);
        return JSON.toJSONString(new Result().setCode(200).setMessage("注册成功"));
    }


    /**
     * 检测注册的用户名是否已被注册
     *
     * @param username
     * @return
     */
    @GetMapping("/checkUsername")
    public String checkUsername(String username) {
        return JSON.toJSONString(new Result().setCode(200).setMessage("用户名被注册的状态").setData(registerService.checkUsername(username)));
    }

    @GetMapping("/checkNickName")
    public String checkNickName(String nickName) {
        return JSON.toJSONString(new Result().setCode(200).setMessage("昵称被注册的状态").setData(registerService.checkNickName(nickName)));
    }

}
